<?php
/**
 * CRMLite Default Admin Controller
 *
 * @final
 * @package Crmlite_App
 * @version $Id:$
 * @copyright 2006-2007 ETE.CN
 * @author Yi BO  <etng2004@gmail.com>
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
require_once dirname(__FILE__) . '/AdminBaseController.php';
class Admin_IndexController extends AdminBaseController
{
	public function indexAction()
	{
		$this->setTitle('');
		$this->_forward('list','announce');
	}

	public function logoutAction()
	{
		$SM = new StaffModel();
		if($SM->logout())
		{
			$this->feedback("已成功注销本次登录，等待转向系统首页！", '成功', $this->module_url);
		}
	}

	public function latestContactsAction()
	{

	}



	public function loginAction()
	{
		$message = '';
		if($this->isPost())
		{
			$SM = new StaffModel();
			$record = $_POST['fields'];
			if(!isset($record['captcha']) || $record['captcha']!=$this->session->captcha)
			{
			    $message =  'MISSING_CAPTCHA';
			}
			else
			{
    			if($SM->login($record['username'], $record['userpass']))
    			{
    				$this->feedback("登录成功，等待转向系统首页！", '成功', $this->module_url);
    			}
    			$message = $SM->getErrors();
			}
		}

		$this->setTitle("登录");
		$this->view->message = $message;
	}
	public function messageAction()
	{
		$message = $this->_getParam('message', 'Unknown notice accrued.');
		$url = $this->_getParam('url', '/');
		$title = $this->_getParam('title', 'Notice');
		$icon = $this->_getParam('icon', 'note');
		$this->view->message = $message;
		$this->view->url = $url;
		$this->view->title = $title;
		$this->view->icon = $icon;
		$this->setTitle("系统提示");
	}
	/**
	 * 解析开发者博客种子内容并展示出来
	 *
	 */
	public function developerNewsAction()
	{
		$this->view->feed = $this->parseFeed('http://www.etng.net/blog/feed/');;
	}
	/**
	 * 解析订阅地址并返回订阅详细信息
	 *
	 * @param string $feed_url
	 * @return array
	 */
	protected function parseFeed($feed_url)
	{
		$Cache = Zend_Registry::get('cache');
		/* @var $Cache Zend_Cache_Core */
		$cache_id = md5('feed:' . $feed_url);
		$feed = $Cache->load($cache_id);
		if(!$feed instanceof Zend_Feed_Abstract)
		{
			$this->log("Feed $feed_url missing,refetch!");
			try {
				$feed = Zend_Feed::import($feed_url);
				/* @var $feed Zend_Feed_Abstract */
				assert($feed instanceof Zend_Feed_Abstract);
			} catch (Zend_Feed_Exception $e) {
				echo "Exception caught importing feed: {$e->getMessage()}\n";
				exit;
			}
			$this->log("Feed $feed_url fetched,cache it!");
			$Cache->save($feed, $cache_id);
		}
		// Initialize the channel data array
		$channel = array(
		'title'		=> $feed->title(),
		'link'		=> $feed->link(),
		'description'=> $feed->description(),
		'count'		=> $feed->count(),
		'rss'		=> $feed_url,
		/**
		 * optional below
		 */
		 'category' => $feed->category(),
		 'language'	=> $feed->language(),
		 'pubdate'	=> date( 'Y-m-d H:i:s' , strtotime($feed->pubDate())),
		 'items'	=> array()
		 );

		 /**
		 * 循环处理 Feed
		 */
		 foreach ($feed as $item) {
		 	if(!($author 		= $item->author()))
		 	{
		 		$author = $item->creator();
		 	}
		 	if(!($category 	= $item->category()))
		 	{
		 		$category = $this->DomNodes2Array($item->subject());
		 	}
		 	$channel['items'][] = array(
		 	'title'			=> $item->title(),
		 	'link'			=> $item->link(),
		 	'description'	=> $item->description(),
		 	'author' 		=> $author,
		 	'category'		=> $category,
		 	'comments'		=> $item->comments(),
		 	'comment_rss'	=> $item->commentRss(),
		 	'pubdate'		=> strtotime($item->pubDate())
		 	);
		 }
		 return $channel;
	}

	/**
	 * 将包含多个相同类型的子节点(DOMElement)转化成内容组成的数组
	 *
	 * @param array $dom_nodes
	 * @return array
	 */
	protected function DomNodes2Array($dom_nodes)
	{
		$array = array();
		if(is_array($dom_nodes))
		{
			foreach ($dom_nodes as $dom_node)
			{
				if($dom_node instanceof DOMElement)
				{
					$array []= $dom_node->nodeValue;
				}
			}
		}
		else
		{
			$array[] = $dom_nodes;
		}
		return $array;
	}
}
